"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.setSwaggerJSONByPathMeta = exports.setSwaggerJSONByModelMeta = void 0;
var index_1 = __importDefault(require("./index"));
var router_1 = require("../router");
// 将 model 和 list 处理成 schema 对象
function getJsomSchema(a, b) {
    var list = [];
    var model = null;
    if (Array.isArray(a)) {
        list = a;
        model = b;
    }
    else if (a) {
        model = a;
    }
    var result = {};
    result.type = 'object';
    // 单个 Model
    if (model) {
        Object.assign(result, model.type);
    }
    // 逐个设置参数
    if (list.length) {
        Object.assign(result, {
            properties: (function () {
                var properties = {};
                list.reverse().forEach(function (paramMeta) {
                    properties[paramMeta.name] = paramMeta;
                });
                return properties;
            })()
        });
    }
    return result;
}
// 将 model 和 list 合并成一个列表
function getMegeToList(operationMeta, a, b) {
    var list = [];
    var model = null;
    if (Array.isArray(a)) {
        list = a;
        model = b;
    }
    else if (a) {
        model = a;
    }
    var SwaggerJSON = index_1.default.getSwaggerJSON();
    if (model) {
        var modelName = '';
        // model 数组 或者对象 不合并
        if (model.type && (model.type.type === 'array' || model.type.type === 'object')) {
            if (model.type.type === 'array') {
                return model;
            }
            else if (model.type.type === 'object') {
                return list;
            }
        }
        try {
            modelName = model.type.$ref.split('/')[2];
        }
        catch (error) {
            throw new Error('model.type.$ref 不正确');
        }
        var properties_1 = SwaggerJSON.definitions[modelName].properties;
        Object.keys(properties_1).reverse().forEach(function (key) {
            var item = properties_1[key];
            list.push({
                name: key,
                description: item.description,
                type: item.type,
                default: item.default,
                // 实体类没有 in 属性，直接根据请求方式来设置
                // in: [DataFormat.FORMDATA, DataFormat.FORMDATA_CUSTOM, DataFormat.JSON].includes(operationMeta.dataFormat) ? 'formData' : 'query'
                in: operationMeta.httpMethod === 'get' ? 'query' : 'formData'
            });
        });
    }
    return list.map(function (item) { return Object.assign({}, item, typeof item.type === 'object' ? item.type : {}, {
        in: item.in,
        // param 参数必填
        required: item.in === 'path' ? true : item.required
    }); });
}
function setSwaggerJSONByModelMeta(modelMeta) {
    var SwaggerJSON = index_1.default.getSwaggerJSON();
    modelMeta.isCreateTag && SwaggerJSON.tags.push({
        name: modelMeta.name,
        description: modelMeta.description
    });
    // 添加实体类
    if (!modelMeta.isCreateModel)
        return;
    if (SwaggerJSON.definitions[modelMeta.name]) {
        throw new Error(modelMeta.name + " Model \u5DF2\u5B58\u5728");
    }
    SwaggerJSON.definitions[modelMeta.name] = {
        description: modelMeta.description,
        type: 'object',
        // 设置实体类属性
        properties: (function () {
            var result = {};
            modelMeta.modelPropertyMetaList.forEach(function (modelPropertyMeta) {
                if (result[modelPropertyMeta.property]) {
                    throw new Error(modelMeta.name + " Model " + modelPropertyMeta.property + " \u5DF2\u5B58\u5728");
                }
                else {
                    result[modelPropertyMeta.property] = Object.assign(modelPropertyMeta.type, {
                        description: modelPropertyMeta.description,
                        default: modelPropertyMeta.default
                    });
                }
            });
            return result;
        })()
    };
}
exports.setSwaggerJSONByModelMeta = setSwaggerJSONByModelMeta;
function setSwaggerJSONByPathMeta(pathMeta) {
    var SwaggerJSON = index_1.default.getSwaggerJSON();
    // 设置 SwaggerJson 路径
    pathMeta.operationMetaList.forEach(function (operationMeta) {
        var _a;
        var path = (_a = {},
            _a[operationMeta.httpMethod] = {
                tags: pathMeta.tags,
                summary: operationMeta.summary,
                description: operationMeta.description,
                consumes: [operationMeta.dataFormat.trim()],
                produces: [operationMeta.dataFormat.trim()],
                parameters: (function () {
                    if (operationMeta.dataFormat === router_1.DataFormat.JSON && operationMeta.httpMethod === 'post') {
                        return [{
                                name: 'body',
                                in: 'body',
                                description: operationMeta.parameterModel ? operationMeta.parameterModel.description : '',
                                required: true,
                                schema: getJsomSchema(getMegeToList(operationMeta, operationMeta.parameterList, operationMeta.parameterModel), null)
                            }];
                    }
                    else {
                        return getMegeToList(operationMeta, operationMeta.parameterList, operationMeta.parameterModel);
                    }
                })(),
                responses: { 200: {
                        description: '',
                        schema: (function () {
                            if (operationMeta.responseModel || operationMeta.responseList.length) {
                                return getJsomSchema(getMegeToList(operationMeta, operationMeta.responseList, operationMeta.responseModel), null);
                            }
                            else if (index_1.default.DefaultResponseModel) {
                                return { $ref: "#/definitions/" + index_1.default.DefaultResponseModel };
                            }
                            else {
                                return { type: 'string' };
                            }
                        })()
                    } }
            },
            _a);
        var url = pathMeta.path + operationMeta.path;
        // 处理地址中的 param 参数
        if (url.includes(':')) {
            var keywords = url.split('/').filter(function (item) { return item.includes(':'); });
            keywords.forEach(function (key) {
                url = url.replace(key, "{" + key.replace(':', '') + "}");
            });
        }
        SwaggerJSON.paths[url] = path;
    });
}
exports.setSwaggerJSONByPathMeta = setSwaggerJSONByPathMeta;
